/**
 * 
 */
package com.heer.RD.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.Assert;

/**
 * @author zhao.zehui
 *
 */
public class UtilsMisc {

	public static boolean isDate(String date) {
		return isDate(date, "yyyy-MM-dd");
	}
	public static boolean isDate(String date, String format) {
		if(format == null)
			return false;
		SimpleDateFormat sf = new SimpleDateFormat(format);
		try {
			sf.parse(date);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}
	public static String getCurrDateStr() {
		return getCurrDateStr("yyyy-MM-dd");
	}
	public static String getCurrDateStr(String format) {
		if(format == null)
			return null;
		SimpleDateFormat sf = new SimpleDateFormat(format);
		return sf.format(new Date());
	}
	public static String getCurrTimeStr() {
		return getCurrDateStr("yyyy-MM-dd HH:mm:ss");
	}
	
	public static Map toMap(Object...obj) {
		Map m = new HashMap();
		if(obj.length % 2 != 0) {
			return null;
		}
		for (int i = 0; i < obj.length; ) {
			m.put(obj[i++], obj[i++]);
		}
		return m;
	}

	public static List toList(Object...obj) {
		List l = new ArrayList();
		for (int i = 0; i < obj.length; i++) {
			l.add(obj[i]);
		}
		return l;
	}
	
	
	public static boolean checkEmail(String email) {
	        
	    Pattern pattern = Pattern.compile("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+");
	    Matcher matcher = pattern.matcher(email);
	    if (matcher.matches()) {
	        return true;
	    }
	    return false;
	}
	
	//将工时转换成人天
	public static int HOUR_PER_DAY = 8;
	public static String transHour(Object obj) {
		if(obj == null)
			return "0";
		
		DecimalFormat df = new DecimalFormat();
		df.setMaximumFractionDigits(2);
		
		try {
			return df.format(((Number)obj).doubleValue() / HOUR_PER_DAY);
		} catch (Exception e) {
			return "0";
		}
		
	}
	
	/**
	 * 得到某年某周的日子，返回长度为7的日子数字，每周从周一开始算
	 * @param theyear
	 * @param theweek
	 * @return
	 */
	public static String[] getWeekRange(int theyear, int theweek) {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, theyear);
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		c.set(Calendar.WEEK_OF_YEAR, theweek);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		String[] days = new String[7];
		for (int i = 0; i < days.length; i++) {
			days[i] = sdf.format(c.getTime());
			c.add(Calendar.DAY_OF_WEEK, 1);
		}
		
		return days;
	}
	/**
	 * 得到当前周数
	 * @return
	 */
	public static int getCurrWeekNum() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		
		return c.get(Calendar.WEEK_OF_YEAR);
	}
	/**
	 * 根据起始周数，得到这区间内的日子
	 * 以二维数组，按星期（从周一~周日）的方式保存
	 * @param beginWeek
	 * @param endWeek
	 * @return
	 */
	public static String[][] getWeekRange(int theyear, int beginWeek, int endWeek) {
		Assert.notNull(beginWeek);
		Assert.notNull(endWeek);
		Assert.isTrue(endWeek >= beginWeek);
		
		String[][] days = new String[endWeek - beginWeek + 1][7];
		for (int i = beginWeek; i < endWeek + 1; i++) {
			days[i - beginWeek] = getWeekRange(theyear, i);
		}
		return days;
	}
	
	
	public static void main(String[] args) {
		System.out.println(getWeekRange(2011, 1)[0]+"~"+getWeekRange(2011, 1)[6]);
		System.out.println(getWeekRange(2011, 8)[0]+"~"+getWeekRange(2011, 8)[6]);
		System.out.println(getWeekRange(2011, 9)[0]+"~"+getWeekRange(2011, 9)[6]);
		
//		System.out.println(getLastYearOfWeek(2011, 1));
//		System.out.println(getFirstYearOfWeek(2011, 7));
		System.out.println(getWeekRange(2011, getCurrWeekNum())[0]+"~"+getWeekRange(2011, getCurrWeekNum())[6]);
		String[][] ds = getWeekRange(Calendar.getInstance().get(Calendar.YEAR), 15, 16);
		for (int i = 0; i < ds.length; i++) {
			for (int j = 0; j < ds[i].length; j++) {
				System.out.print(ds[i][j]+",");
			}
			System.out.println("");
		}
	}
}
